<template>
  <div class="login-wrap">
    <div class="title">xxxx管理系统</div>
    <div class="bg">
      <div class="ms-login">
        <el-form :model="loginForm" :rules="rules" ref="formRef" label-width="90px" class="demo-loginForm ms-content" v-loading="loading" @submit.prevent @keyup.enter="submitForm">
          <el-form-item label="账号：" prop="username">
            <el-input v-model.trim="loginForm.username" autocomplete="on"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input type="password" v-model.trim="loginForm.password" autocomplete="on"></el-input>
          </el-form-item>
          <el-form-item label="验证码：" prop="code">
            <div class="display-flex">
              <el-input v-model.trim="loginForm.code" placeholder="请填写验证码"></el-input>
              <el-button text type="primary" class="ml14" :disabled="disabledbtn1" @click="handleGetCode">{{ disabledbtn1 ? time + "s后可重发" : "获取验证码" }} </el-button>
            </div>
          </el-form-item>
          <div id="captcha" class="captcha"></div>
          <div class="login-btn">
            <el-button type="primary" :icon="disabledbtn ? 'el-icon-loading' : ''" :disabled="disabledbtn" style="width: 100%" @click="submitForm">{{ loadName }} </el-button>
          </div>
        </el-form>
      </div>
    </div>
    <div class="wzg-foot">
      <div class="wzg-foot-content">浙ICP备19029513号-1</div>
      <div class="wzg-foot-content">Copyright © 2021 xxxx电子有限公司</div>
    </div>
  </div>
</template>

<script setup lang="ts" name="login">
import type { ILoginForm, ILoginFormResult } from "@/types/login";
import { postLogin, getMobileSmsCodeInfo } from "@/utils/api/login.js";
import { isPhone } from "@/utils/utils";

const loading = ref(false);
const disabledbtn = ref(false);
const disabledbtn1 = ref(false);
const time = ref(60);
const intervalID = ref<number | undefined>(undefined);
const router = useRouter();
const loginForm = reactive<ILoginForm>({
  code: "",
  username: "",
  password: "",
  validate: ""
});
const rules = {
  username: [{ required: true, message: "请填写用户名", trigger: "blur" }],
  password: [{ required: true, message: "请填写密码", trigger: "blur" }],
  code: [{ required: true, message: "请填写验证码", trigger: "blur" }]
};

const formRef = useTemplateRef("formRef");

const loadName = computed(() => {
  return disabledbtn.value ? "登录中..." : "登录";
});

function submitForm() {
  if (!formRef.value) return;
  formRef.value.validate(valid => {
    if (!valid) {
      ElMessage.warning("请填写必填字段");
      return;
    }
    handleLogin();
  });
}
/* 获取验证码 */
function handleGetCode() {
  if (!handleValidate()) return;
  disabledbtn1.value = true;
  /* 倒计时 */
  time.value = 60;
  intervalID.value = setInterval(handleCountdown, 1000);
  handleMobileSmsCodeInfo();
}

/* 验证手机号 */
function handleValidate() {
  if (loginForm.username == "") {
    ElMessage.warning("请填写手机号");
    return false;
  }
  if (!isPhone(loginForm.username)) {
    ElMessage.error("请填写正确手机号");
    return false;
  }
  return true;
}

/* 获取验证码接口 */
function handleMobileSmsCodeInfo() {
  getMobileSmsCodeInfo({ mobile: loginForm.username })
    .then(() => {
      ElMessage.success("验证码发送成功");
    })
    .catch(() => {
      disabledbtn1.value = false;
      clearInterval(intervalID.value);
    });
}

/* 倒计时 */
function handleCountdown() {
  time.value--;
  if (time.value <= 0) {
    disabledbtn1.value = false;
    clearInterval(intervalID.value);
  }
}

/* 登录 */
function handleLogin() {
  disabledbtn.value = true;
  loading.value = true;
  postLogin<ILoginFormResult, ILoginForm>(loginForm, "POST")
    .then(res => {
      ElMessage.success("登录成功");
      localStorage.setItem("Authorization", res.result.accessToken);
      localStorage.setItem("refreshToken", res.result.refreshToken);
      console.log(router.currentRoute.value);
      const redirect = router.currentRoute.value.query.redirect;
      console.log(redirect);
      /* 重新登录返回原有的页面位置（如果存在的话） */
      if (redirect) {
        router.push(redirect as string);
      } else {
        router.push("/");
      }
    })
    .catch(error => {
      console.log(error);
    })
    .finally(() => {
      disabledbtn.value = false;
      loading.value = false;
    });
}
</script>

<style scoped>
.title {
  position: absolute;
  left: 50%;
  top: 23%;
  font-size: 35px;
  text-align: center;
  color: #fff;
  transform: translateX(-50%);
}

.title-english {
  top: 25%;
  color: #777d8e;
  font-size: 14px;
}

.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(http://img.zgwstxc.com/admin-bg.jpg) no-repeat;
  background-size: cover;
}

.ms-title {
  width: 100%;
  border-bottom: 1px solid #ddd;
  text-align: center;
  font-size: 20px;
  color: #fff;
  line-height: 50px;
}

.bg {
  position: absolute;
  left: 50%;
  top: 31%;
  width: 400px;
  margin-top: 10px;
  background-color: #409eff;
  border-radius: 5px;
  transform: translateX(-50%);
}

.ms-login {
  margin: 10px;
  background: rgba(255, 255, 255, 0.9);
}

.ms-content {
  padding: 30px 30px 30px 25px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 30%;
  height: 36px;
  margin-top: 10px;
  background-color: #4c9eff;
  font-size: 14px;
}

.login-tips {
  font-size: 12px;
}

.lf {
  float: left;
}

.box {
  min-width: 350px;
  margin-left: 50px;
  width: 30%;
}

.rf {
  float: right;
}

.wzg-foot {
  position: absolute;
  left: 50%;
  bottom: 0;
  margin: 48px 0 24px 0;
  text-align: center;
  transform: translateX(-50%);
}

.wzg-foot-bottom {
  margin-bottom: 5px;
}

.wzg-foot .wzg-foot-content {
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.wzg-foot .wzg-foot-content span {
  color: rgba(255, 255, 255, 0.9);
}

.captcha_img {
  margin-bottom: 20px;
  text-align: left;
  cursor: pointer;
}

.captcha_img img {
  width: 90px;
  height: 32px;
  vertical-align: middle;
}

.display-flex {
  display: flex;
}

.ml14 {
  margin-left: 14px;
}
</style>
<style>
.el-input__inner {
  box-shadow: 0 0 0 1000px white inset !important;
}
</style>
